Imports System.Data.OleDb
Partial Public Class wfrmCadBanner
    Inherits System.Web.UI.Page
    Public script As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            Me.encheBanners()
        End If
    End Sub

    Private Sub mostraDados()
        Dim obj As New SiteDLL.Banner()
        Try
            obj.IDBanner = Val(txtIdbanner.Text)
            obj.carregar()
            imgBanner.ImageUrl = obj.Img
            txtUrl.Text = obj.Url
            txtTooltip.Text = obj.ToolTip
            ddlTamanho.SelectedIndex = ddlTamanho.Items.IndexOf(ddlTamanho.Items.FindByValue(obj.Tamanho))

            If obj.TemRegistro Then
                btnRemover.Enabled = True
            Else
                btnRemover.Enabled = False
            End If

        Catch ex As Exception
            Response.Write(ex.Message)
        Finally
            obj.Dispose()
            obj = Nothing
        End Try

    End Sub
    Protected Sub btnSalvar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSalvar.Click
        Dim fun As New Funcoes

        Dim obj As New SiteDLL.Banner
        obj.IDBanner = Val(txtIdbanner.Text)
        obj.carregar()
        Try

            obj.Img = imgBanner.ImageUrl
            obj.Url = Trim(txtUrl.Text)
            obj.ToolTip = txtTooltip.Text
            obj.Tamanho = ddlTamanho.SelectedValue
            obj.Fixo = "Sim"

            obj.salvar()
            obj.efetivarTransacao()
            script += fun.MsgboxAviso(obj.Aviso)
        Catch ex As Exception
            script += fun.MsgboxAviso(ex.Message)
        Finally
            obj.Dispose()
            obj = Nothing
            fun = Nothing
        End Try

    End Sub

    Protected Sub LinkButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
        tbBanners.Visible = False
        tbCadBanner.Visible = True

        txtIdbanner.Text = ""
        '
        limpaCampos()
    End Sub

    Protected Sub LinkButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton2.Click
        tbBanners.Visible = True
        tbCadBanner.Visible = False

    End Sub

    Protected Sub btnRemover_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemover.Click
        Dim fun As New Funcoes
        Dim obj As New SiteDLL.Banner
        obj.IDBanner = Val(txtIdbanner.Text)
        obj.carregar()
        Try
            If obj.TemRegistro Then
                obj.excluir()
                obj.efetivarTransacao()
                script += fun.MsgboxAviso(obj.Aviso)
            End If
        Catch ex As Exception
            script += fun.MsgboxAviso(ex.Message)
        Finally
            obj.Dispose()
            obj = Nothing
            fun = Nothing

        End Try
    End Sub

    Protected Sub txtIdbanner_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtIdbanner.TextChanged
        mostraDados()

    End Sub

    Protected Sub btnEnviar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnviar.Click
        Dim fun As New Funcoes
        Try
            FileUpload1.SaveAs(Server.MapPath("/Icli/Banner/" & FileUpload1.FileName))
            imgBanner.ImageUrl = "/Icli/Banner/" & FileUpload1.FileName
        Catch ex As Exception
            script += fun.MsgboxAviso(ex.Message)
        Finally
            fun = Nothing
        End Try
    End Sub

    Private Sub DataList1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles DataList1.ItemCommand
        If e.CommandName = "Selecionar" Then
            Dim lblID As Label = e.Item.FindControl("lblID")
            Me.txtIdbanner.Text = lblID.Text
            mostraDados()
            Me.tbBanners.Visible = False
            Me.tbCadBanner.Visible = True
        End If
    End Sub

    Protected Sub ddlFiltraTamanho_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlFiltraTamanho.SelectedIndexChanged
        Me.encheBanners(Me.ddlFiltraTamanho.SelectedValue)
    End Sub

    Public Sub encheBanners(Optional ByVal t As String = "P")
        Dim c As New SiteDLL.Banner
        Try
            c.addWhere("Fixo", ClasseBase.Classes.PersistClass.TipoFiltro.Igual, "Sim")
            c.addWhere(ClasseBase.Classes.PersistClass.OperadorLogico.AND, "Tamanho", ClasseBase.Classes.PersistClass.TipoFiltro.Igual, t)

            DataList1.DataSource = c.getTodosRegistros
            DataList1.DataBind()

            DataList1.DataSource = c.getTodosRegistros
            DataList1.DataBind()

            c.Dispose()
            c = Nothing
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub btnLimpar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLimpar.Click
        limpaCampos()
    End Sub

    Private Sub limpaCampos()
        Me.txtIdbanner.Text = String.Empty
        Me.imgBanner.ImageUrl = String.Empty
        Me.txtUrl.Text = String.Empty
        Me.txtTooltip.Text = String.Empty
    End Sub
End Class